package com.easpeed.comon.socket_tool

import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException


/**
 * Created by bigyu on 2025-06-26 18:33
 * com.easpeed.comon.socket_tool
 */
object SocketTools {


    @JvmStatic
    fun log(msg: String) {
        SocketClient.instance.sendMsg(msg)
    }


    fun getLocalIpAddress(): String {
        try {
            val en = NetworkInterface.getNetworkInterfaces()
            while (en.hasMoreElements()) {
                val intf = en.nextElement()
                val enumIpAddr = intf.inetAddresses
                while (enumIpAddr.hasMoreElements()) {
                    val inetAddress = enumIpAddr.nextElement()
                    if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                        return inetAddress.getHostAddress()
                    }
                }
            }
        } catch (ex: SocketException) {
            ex.printStackTrace()
        }
        return "127.0.0.1"
    }

}